package com.yeban.bams.client.clientsocket;

import com.yeban.bams.server.serversocket.RequestTO;
import com.yeban.bams.server.serversocket.ResponseTO;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @Desc 客户端网络层
 * @Author yeban
 * @Date 2025/11/7
 */
public class ClientRequest {

    // 向服务器发送请求并获取响应
    public static ResponseTO send(RequestTO request) {
        try(Socket socket = new Socket()) {
            java.net.InetSocketAddress addr = new java.net.InetSocketAddress("192.168.0.108", 8888);
            socket.connect(addr, 2000);
            socket.setSoTimeout(5000);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.flush();
            oos.writeObject(request);
            oos.flush();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ResponseTO response = (ResponseTO)ois.readObject();
            ois.close();
            oos.close();
            return response;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return new ResponseTO(false, null, "客户端请求异常：" + e.getMessage());
        }
    }
}
